package pack01_zookeeper;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.cache.TreeCache;
import org.apache.curator.framework.recipes.cache.TreeCacheEvent;
import org.apache.curator.framework.recipes.cache.TreeCacheListener;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.junit.Test;

/**
 * @author lilulu
 * @date 2023/2/9 14:09
 */
public class Demo01Practice {

    @Test
    public void createZNode() throws Exception {
        //1:定制一个重试策略
        ExponentialBackoffRetry exponentialBackoffRetry = new ExponentialBackoffRetry(1000, 1);
        //2:获取一个客户端对象
        String connectStr = "node1:2181,node2:2181,node3:2181";
        CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient(connectStr, exponentialBackoffRetry);
        //3:开启客户端
        curatorFramework.start();
        //4:创建节点
        String node = curatorFramework.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath("/app2", "hadoop".getBytes());
        System.out.println("创建的节点信息：" + node);
        //5:关闭客户端
        curatorFramework.close();
    }


    @Test
    public void getZNode() throws Exception {
        //1:定制一个重试策略
        ExponentialBackoffRetry exponentialBackoffRetry = new ExponentialBackoffRetry(1000, 1);
        //2:获取一个客户端对象
        String connectStr = "node1:2181,node2:2181,node3:2181";
        CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient(connectStr, exponentialBackoffRetry);
        //3:开启客户端
        curatorFramework.start();
        //4:创建节点
//        String node = curatorFramework.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath("/app2", "hadoop".getBytes());
        byte[] bytes = curatorFramework.getData().forPath("/app2");
        System.out.println("节点的数据是：" + new String(bytes));
        //5:关闭客户端
        curatorFramework.close();
    }


    @Test
    public void setZNode() throws Exception {
        //1:定制一个重试策略
        ExponentialBackoffRetry exponentialBackoffRetry = new ExponentialBackoffRetry(1000, 1);
        //2:获取一个客户端对象
        String connectStr = "node1:2181,node2:2181,node3:2181";
        CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient(connectStr, exponentialBackoffRetry);
        //3:开启客户端
        curatorFramework.start();
        //4:创建节点
//        String node = curatorFramework.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath("/app2", "hadoop".getBytes());
        curatorFramework.setData().forPath("/app2", "newData".getBytes());
        //5:关闭客户端
        curatorFramework.close();
    }


    @Test
    public void deleteZNode() throws Exception {
        //1:定制一个重试策略
        ExponentialBackoffRetry exponentialBackoffRetry = new ExponentialBackoffRetry(1000, 1);
        //2:获取一个客户端对象
        String connectStr = "node1:2181,node2:2181,node3:2181";
        CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient(connectStr, exponentialBackoffRetry);
        //3:开启客户端
        curatorFramework.start();
        //4:创建节点
//        String node = curatorFramework.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath("/app2", "hadoop".getBytes());
        curatorFramework.delete().forPath("/app2");
        //5:关闭客户端
        curatorFramework.close();
    }

    @Test
    public void znodeWatch() throws Exception {

        //1:创建客户端和服务器之间的重试策略

        RetryPolicy backoffRetry =
                new ExponentialBackoffRetry(3000, 3);

        //2:获取客户端对象
        String serverList = "node1:2181,node2:2181,node3:2181";
        CuratorFramework client = CuratorFrameworkFactory.newClient(serverList, backoffRetry);

        //3:启动客户端
        client.start();

        //4:将要监听的节点树存入缓存中
        TreeCache treeCache = new TreeCache(client, "/app2");

        //5:自定义监听
        treeCache.getListenable().addListener(new TreeCacheListener() {
            public void childEvent(CuratorFramework curatorFramework, TreeCacheEvent treeCacheEvent) throws Exception {
                //因为不管是：增加/删除/修改节点，都会执行该方法，但是具体是因为哪一种才执行该方法，则需要进一步确认
                switch (treeCacheEvent.getType()) {
                    case NODE_ADDED:
                        System.out.println("监控到增加节点事件!");
                        System.out.println("有客户端上线了!");
                        break;
                    case NODE_REMOVED:
                        System.out.println("监控到节点移除事件!");
                        System.out.println("主节点挂掉了!");
                        System.out.println("让备用节点称为新的主节点!");
                        break;
                    case NODE_UPDATED:
                        System.out.println("监控到节点修改事件!");
                        //重新读取配置文件
                        break;
                    case CONNECTION_SUSPENDED:
                        break;
                    case CONNECTION_RECONNECTED:
                        break;
                    case CONNECTION_LOST:
                        break;
                    case INITIALIZED:
                        break;
                }
            }
        });

        //开启监听
        treeCache.start();

        //让程序挂起
        while (true);
        //Thread.sleep(100000000);
    }


}
